package com.kh.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kh.reggie.common.Result;
import com.kh.reggie.entity.OrderDetail;

import com.kh.reggie.entity.Orders;
import com.kh.reggie.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * 订单表明细
 */
@RestController
@RequestMapping("/order")
public class OrdersController {
   @Autowired
   private OrdersService ordersService;

   @GetMapping("/page")
   public Result<Page> page(int page, int pageSize, String number,String beginTime, String endTime){
      Page<Orders> ordersPage = new Page<>(page, pageSize);
      //        条件构造器
      LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper();
//        添加过滤条件
      queryWrapper.like(StringUtils.isNotBlank(number),Orders::getNumber,number);
      // 使用 StringUtils.isNotBlank 判断字符串是否不为空
      if (StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
         // 添加 BETWEEN 条件
         queryWrapper.between(Orders::getOrderTime, beginTime, endTime);
      } else if (StringUtils.isNotBlank(beginTime)) {
         // 只有开始时间
         queryWrapper.ge(Orders::getOrderTime, beginTime);
      } else if (StringUtils.isNotBlank(endTime)) {
         // 只有结束时间
         queryWrapper.le(Orders::getOrderTime, endTime);
      }
//        添加排序条件
      queryWrapper.orderByDesc(Orders::getOrderTime);
//        执行查询
      ordersService.page(ordersPage,queryWrapper);
      return Result.success(ordersPage);
   }

   /**
    * 派送
    * @param orders
    * @return
    */
   @PutMapping
   public Result<String> out1(@RequestBody Orders orders){
      ordersService.statusChange(orders);
      return Result.success("派送成功");
   }
}
